<?php
/*
 * Created on 4 nov. 07
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 /*
  * 
  * Model
  * 
 
class Menu < ActiveRecord::Base

#validates_presence_of :name
validates_length_of :name, :in => 2..12, :message => "Name's length in %d"
validates_numericality_of :parent_id, :only_integer => true
validates_numericality_of :position, :only_integer => true

##Ajax reorder
  def self.reorder(serialized_list)
    self.transaction do
      serialized_list.each_with_index do |cid,index|
        find(cid).update_attribute "position", index
      end
    end
  end
  
end

  *
  *Controller
  *

class Admin::MenuController < Admin::BaseController

  def index
    list
    render_action 'list'
  end
  
  def list
    @menus = Menu.find(:all, :order => :position)
  end
  
  def new
    @menu = Menu.new(params[:menu])
    @menus = Menu.find(:all, :conditions => "parent_id = 0")
    #@menus += [:name => "yoy", :id => "0"]
    @contents = Content.find(:all, :order => :type, :select => "type, title, permalink, name, published_at")

    if request.post? 
      if @menu.save
        flash[:notice] = 'Menu was successfully created.'
        redirect_to :action => 'list'
      else
        flash[:error] = 'Menu could not be created.'
      end
    end
  end
  
  def edit
    @menu = Menu.find(params[:id])
    @menu.attributes = params[:menu]
    @menus = Menu.find(:all, :conditions => "parent_id = 0")
    @contents = Content.find(:all, :order => :type, :select => "type, title, permalink, name, published_at")
    
    if request.post? and @menu.save
      flash[:notice] = 'Menu was successfully updated.'
      redirect_to :action => 'list'
    end
  end
  
  def destroy
    #@menu = Menu.find(params[:id])
    menus = Menu.find(:all, :conditions => ["parent_id = :id or id = :id", {:id => params[:id]}])
    if request.post?
      for menu in menus
        menu.destroy
      end
      flash[:notice] = 'Menu and childs has been destroyed.'
      redirect_to :action => 'list'
    end
  end
  
  ## Ajax ordering
  def order
    if !params[:id].nil?
      menu_param = params["menu_list_"+params[:id]]
    else
      menu_param = params[:menu_list]
    end
    Menu.reorder(menu_param)
    render :nothing => true
  end
  
end

  *
  * View : List
  *   

  <% @page_heading = _('Manage Menu') %>
  
  <% content_for('tasks') do -%>
  	<%= tab _("Create menu entry"), :controller => "menu", :action => "new" %>
  <% end -%>
  
  <h4>How to manage menu :</h4>
  <p>Create menu entry first then choose a parent to create sub-menu.
  You can manage position here with drag and drop. You can't change the parent of a sub-menu, 
  you should edit it.</p>
  
  <hr />
  
  <ul id="menu_list">
  <% for menu in @menus -%>
  
  	<% if menu.parent_id == 0 %>
  	<li id="menu_<%= menu.id %>">
  		<%= menu.name %> <%= link_to_edit menu %> <%= link_to image_tag('delete.png'), {:action => 'destroy', :id => menu}, :confirm => _("Are you sure to destroy this menu entry and its childs?"), :post => :true %>
  		<ul id="menu_list_<%= menu.id %>">
  		<% for sub in @menus -%>
  			<% if sub.parent_id == menu.id %>
  			<li id="menu_<%= sub.id %>"><%= sub.name %> <%= link_to_edit sub %> <%= link_to image_tag('delete.png'), {:action => 'destroy', :id => sub}, :confirm => _("Are you sure to destroy this menu entry and its childs?"), :post => :true %></li>
  			<% end -%>
  		<% end -%>
  		</ul>
  		<%= sortable_element "menu_list_" + menu.id.to_s, :url => { :action => "order", :id => menu.id }, :complete => visual_effect(:highlight, "menu_list_" + menu.id.to_s) %>
  	</li>
  	<% end -%>
  <% end -%>
  </ul>
  
  <%= sortable_element "menu_list", :url => { :action => "order" }, :complete => visual_effect(:highlight, "menu_list") %>

  *
  * View : PageList
  * 

<script type="text/javascript" language="javascript">
	function fill_form(name, url){
		document.menu_form.menu_name.value=name;
		document.menu_form.menu_url.value=url;
	}
  </script>
  
  <ul>
  <% for content in @contents -%>
  	<% 	if content.type.to_s == "Page"
  			url = "/pages/"+content.name
  		elsif content.type.to_s == "Article"
			url = "/articles/"+content.published_at.year.to_s+"/"+sprintf("%.2d", content.published_at.month)+"/"+sprintf("%.2d", content.published_at.day)+"/"+content.permalink
  		end %>
  	<li><a href="#" onclick="fill_form('<%= content.title %>', '<%= url %>')">-></a> <%= content.title %></li>
  <% end %>
  </ul>


*/
?>
